'''
Created on 22/02/2012

@author: Bruna
'''

from PyQt4 import QtGui

class Ui_FileChooserDialog(object):
    '''
    File chooser dialog builder
    '''
    
    def setupUi(self, parent):
        self.parent = parent

    def save(self, content):
        '''
        Opens a save dialog and save content
        
        @param content: content to be saved on file
        '''
        file_name, file_extension = QtGui.QFileDialog.getSaveFileNameAndFilter(self.parent, caption="Save File", filter="*.txt")
        file_name = self.__check_extension(file_name, file_extension)
        
        file_handler = open(file_name, 'w')
        file_handler.write(content)
        file_handler.close()
        
    def __check_extension(self, file_name, file_extension):
        '''
        Cheks if file extension should be added
        
        @param file_name: file name
        @param file_extension: file extension
        '''
        if len(file_name) < len(file_extension):
            return file_name + file_extension[1:]
        
        if file_name.split('.')[-1] == file_extension[2:]:
            return file_name
        else:
            return file_name + file_name + file_extension[1:]
        